package com.jswlw.controller.admin.biz;

import com.github.pagehelper.Page;
import com.jswlw.controller.BaseController;
import com.jswlw.entity.biz.ApiFail;
import com.jswlw.service.biz.ApiFailService;
import com.jswlw.service.helper.HttpHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Controller
@RequestMapping("/admin/biz/apiFail")
public class AdminApiFailController extends BaseController {

    @Autowired
    ApiFailService service;
    @Autowired
    HttpHelper httpHelper;

    @RequestMapping("/index")
    public String index(Model model) {
        return "biz/apiFail/index";
    }

    /**
     * 广告列表
     *
     * @param
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestBody ApiFail apiFail) {
        Page<ApiFail> page = service.queryPage(apiFail);
        return pageResult(page.getResult(), page.getTotal());
    }

    @RequestMapping("/reTry")
    @ResponseBody
    public ModelMap reTry(Integer id) {
        try {
            ApiFail apiFail = service.find4d(id);
            if(httpHelper.post(apiFail)){
                service.delete(id);
                return this.successResult("重发成功");

            }else{
                apiFail.setFailCount(apiFail.getFailCount()+1);
                service.update(apiFail);
                return this.failResult("重发失败"+apiFail.getFailContent());
            }
        } catch (Exception e) {
            logger.error("重发失败", e);
            return this.failResult("重发失败" + e.getMessage());
        }
    }
}
